﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Multiselect = true,
                Title = "请选择两个文件",
                Filter = "所有文件(*.*)|*.*"
            };

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] files = openFileDialog.FileNames;
                if (files.Length == 2)
                {
                    string dataDirectory = Path.Combine(Application.StartupPath, "Data");
                    Directory.CreateDirectory(dataDirectory);
                    string outputFile = Path.Combine(dataDirectory, "MergedFile.txt");

                    try
                    {
                        using (StreamWriter writer = new StreamWriter(outputFile))
                        {
                            foreach (string file in files)
                            {
                                string content = File.ReadAllText(file);
                                writer.Write(content);
                            }
                        }

                        MessageBox.Show($"文件合并成功！新文件已保存在：{outputFile}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("请选择两个文件进行合并。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
    }
}
